﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Sherwood.SignOn
{
    public class Cookie
    {
        static HttpCookieCollection _dummyCookies = new HttpCookieCollection();
        
        /// <summary>
        /// Returns the value of the HttpCookie from the current request / response.
        /// If the application is executed outside of a web context, cookies is returned from a dummy collection
        /// </summary>
        public static string GetCookieValue(string name)
        {
            if (HttpContext.Current != null && HttpContext.Current.Items[name] != null)
            {
                return HttpContext.Current.Items[name] as string;
            }
            else
            {
                HttpCookie cookie = null;
                if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.Cookies != null)
                {
                    cookie = HttpContext.Current.Request.Cookies[name];
                    HttpContext.Current.Items[name] = cookie != null ? cookie.Value : null;
                }
                else
                {
                    cookie = _dummyCookies.Get(name);
                }
                if (cookie != null && cookie.Value != null)
                {
                    return cookie.Value;
                }
            }
            return null;

        }

        /// <summary>
        /// Adds a cookie to the HttpResponse
        /// If the application is executed outside of a web context, cookie is added to a dummy collection.
        /// </summary>
        public static void SetCookie(string name, string value)
        {
            SetCookie(name, value, null);
        }

        /// <summary>
        /// Adds a cookie to the HttpResponse
        /// If the application is executed outside of a web context, cookie is added to a dummy collection.
        /// </summary>
        public static void SetCookie(string name, string value, string domain)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.HttpOnly = true;
            cookie.Value = value;
            if (!string.IsNullOrEmpty(domain))
            {
                cookie.Domain = domain;
            }

       /*     //Add cookies to request collection (so that subsequent checks to cookies will return the updated values)
            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.Cookies != null)
            {
                HttpContext.Current.Request.Cookies[cookie.Name] = value;
            }*/

            //Add cookie to response
            if (HttpContext.Current != null && HttpContext.Current.Response != null && HttpContext.Current.Response.Cookies != null)
            {
                /*if (HttpContext.Current.Response.Cookies.AllKeys.Contains(name))
                {
                    HttpContext.Current.Response.Cookies[name].Value = value;
                }
                else
                {*/
                    HttpContext.Current.Response.Cookies.Add(cookie);
                /*}*/
                HttpContext.Current.Items[name] = value;
            }
            else
            {
                _dummyCookies.Set(cookie);
            }
        }

        /// <summary>
        /// Removes a Http cookie
        /// </summary>
        /// <param name="name"></param>
        public static void RemoveCookie(string name)
        {
            /*//Remove cookie from response collection (so that the cookie can no longer be read within the program).
            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.Cookies != null)
            {
                HttpContext.Current.Request.Cookies.Remove(name);
            }*/

            //Remove cookie from response collection.
            if (HttpContext.Current != null && HttpContext.Current.Response != null && HttpContext.Current.Response.Cookies != null)
            {
                HttpContext.Current.Request.Cookies.Remove(name);
                HttpContext.Current.Items.Remove(name);

               /* if (HttpContext.Current.Response.Cookies.AllKeys.Contains(name))
                {
                    HttpContext.Current.Response.Cookies[name].Value = "";
                }
                else
                {*/
                    HttpCookie cookieToRemove = new HttpCookie(name);
                    cookieToRemove.HttpOnly = true;
                    cookieToRemove.Value = "";
                    HttpContext.Current.Response.Cookies.Add(cookieToRemove);
                /*}*/
            }
            else
            {
                _dummyCookies.Remove(name);
            }
        }

        public static void SetPersistentCookie(string name, string value)
        {
            SetPersistentCookie(name, value, null);
        }

        public static void SetPersistentCookie(string name, string value, string domain)
        {
            HttpCookie cookie = new HttpCookie(name);
            cookie.HttpOnly = false;
            cookie.Expires = DateTime.Now.AddMonths(1); //Allow cookie to work for 1 month
            cookie.Value = value;
            if (!string.IsNullOrEmpty(domain))
            {
                cookie.Domain = domain;
            }

            //Add cookie to response
            if (HttpContext.Current != null && HttpContext.Current.Response != null && HttpContext.Current.Response.Cookies != null)
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
                HttpContext.Current.Items[name] = value;
            }
            else
            {   
                _dummyCookies.Set(cookie);
            }
        }
    }
}
